package com.example.administrator.ttphonehelper.battery;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.ttphonehelper.R;

public class BatteryManager1Activity extends AppCompatActivity {
    private TextView mTvValue;
    private TextView mTvStatus;
    private TextView mTvHealth;
    private TextView mTvVoltage;
    private TextView mTvTemperature;
    private ImageView mIvValue;

    private int mStatus;
    private int mHealth;
    private int mValue;
    private int mTotal;
    private int mVoltage;
    private int mTemperature;
    private BatteryBroadcastReceiver mBroadcastReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_battery1);
        init();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //动态注册一个广播
        /*
         * 参一：广播接收者
         * 参二：意图过滤器  此广播接收者要接收的action
         */
        IntentFilter iFilter = new IntentFilter();
        iFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        mBroadcastReceiver = new BatteryBroadcastReceiver();
        registerReceiver(mBroadcastReceiver, iFilter);


    }
    //解除注册
    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mBroadcastReceiver);
    }

    private void init() {
        mTvValue = (TextView) findViewById(R.id.tv_value);
        mTvStatus = (TextView) findViewById(R.id.tv_status);
        mTvHealth = (TextView) findViewById(R.id.tv_health);
        mTvVoltage = (TextView) findViewById(R.id.tv_voltage);
        mTvTemperature = (TextView) findViewById(R.id.tv_temperature);
        mIvValue = (ImageView) findViewById(R.id.iv_battery_icon);
    }

    class BatteryBroadcastReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            //得到电池的充电状态
            mStatus = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0);
            getBatteryStatus();
            //得到电池的健康状态
            mHealth = intent.getIntExtra(BatteryManager.EXTRA_HEALTH,0);
            getBatteryHealth();
            //得到当前电量
            mValue = intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
            //得到总电量
            mTotal = intent.getIntExtra(BatteryManager.EXTRA_SCALE,0);
            //将当前电量转化为百分比
            getCurrValue();
            //得到电压
            mVoltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE,0);
            getVoltage();
            //得到电池温度
            mTemperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
            getTemperature();

            //给控件赋值
            setText();
            setImage();

        }

        private void setImage() {
            if (mValue>=100){
                mIvValue.setImageResource(R.mipmap.bt100);
            }
            else if(mValue>=90){
                mIvValue.setImageResource(R.mipmap.bt90);
            }
            else if (mValue>=80){
                mIvValue.setImageResource(R.mipmap.bt80);
            }
            else if(mValue>=70){
                mIvValue.setImageResource(R.mipmap.bt70);
            }
            else if(mValue>=50){
                mIvValue.setImageResource(R.mipmap.bt50);
            }
            else if(mValue>=30){
                mIvValue.setImageResource(R.mipmap.bt30);
            }
            else if(mValue>=20){
                mIvValue.setImageResource(R.mipmap.bt20);
            }
            else if(mValue>=10){
                mIvValue.setImageResource(R.mipmap.bt10);
            }
            else {
                mIvValue.setImageResource(R.mipmap.bt0);
            }
        }

        private void setText() {
            //当前电量
            mTvValue.setText(getCurrValue());
            //电池状态
            mTvStatus.setText(getBatteryStatus());
            //电池温度
            mTvTemperature.setText(getTemperature());
            //电池电压
            mTvVoltage.setText(getVoltage());
            //电池健康状态
            mTvHealth.setText(getBatteryHealth());
        }


        private String getTemperature() {
            Log.i("info","Temperature"+mTemperature);
            return mTemperature/10+"℃";
        }


        private String getVoltage() {
            Log.i("info","Voltage: "+mVoltage);
            return mVoltage+"Mv";
        }

        private String getCurrValue() {

            return mValue/(float)mTotal*100+"%";
        }

        private String getBatteryStatus() {
            Log.i("info","BatteryStatus: "+mStatus);
            switch (mStatus){
                case BatteryManager.BATTERY_STATUS_CHARGING:
                    return "充电中";
                case BatteryManager.BATTERY_STATUS_DISCHARGING:
                    return "放电中";
                case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                    return "未充电";
                case BatteryManager.BATTERY_STATUS_FULL:
                    return "已充满";
                case BatteryManager.BATTERY_STATUS_UNKNOWN:
                    return "未知状态";
                default:
                    return "未知状态";
            }

        }

        private String getBatteryHealth() {
            Log.i("info","BatteryHealth: "+mHealth);
            switch (mHealth){
                case BatteryManager.BATTERY_HEALTH_GOOD:
                    return "良好";
                case BatteryManager.BATTERY_HEALTH_OVERHEAT:
                    return "过热";
                case BatteryManager.BATTERY_HEALTH_DEAD:
                    return "没电";
                case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
                    return "电压过高";
                case BatteryManager.BATTERY_HEALTH_COLD:
                    return "温度过低";
                case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
                    return "未知错误";
                default:
                    return "未知错误";
            }

        }


    }
}
